var controls=document.getElementsByClassName("left_box")[0].getElementsByTagName("a");
var audio=document.getElementById("audio");
var prev=document.getElementsByClassName("prev")[0];
var next=document.getElementsByClassName("next")[0];
var time=document.getElementsByClassName("time")[0];
var sliding=document.getElementsByClassName("sliding")[0];
var cur=document.getElementsByClassName("cur")[0];
var sing_img=document.getElementsByClassName("head_img")[0].getElementsByTagName("img")[0];
var sing_name=document.getElementsByClassName("message")[0].getElementsByClassName("name")[0];
var sing_writer=document.getElementsByClassName("message")[0].getElementsByClassName("author")[0];
var flag=1,now_sing=0;
var prev_url="";
var next_url="";
audio.src=sing_list[now_sing]["url"];
function update(){
    if(audio.currentTime==0){
        sliding.style.left=-5+"px";
        cur.style.width=0+"px";
    }
    if(!audio.duration){
        time.innerHTML="00:00/00:00";
        if(!sing_list[now_sing]["img"]){
            sing_img.src="./images/default_album.jpg"
        }else{
            sing_img.src=sing_list[now_sing]["img"];
        }
        sing_name.innerHTML=sing_list[now_sing]["name"];
        sing_writer.innerHTML=sing_list[now_sing]["writer"];
    }
    if(!audio.paused){
        controls[1].setAttribute("class","play fl shoutdown");
        controls[2].removeAttribute("class");
        controls[2].setAttribute("class","pause fl");
        time.innerHTML=Math.floor(audio.currentTime/60)+":"+Math.floor(audio.currentTime%60)+"/"+Math.floor(audio.duration/60)+":"+Math.floor(audio.duration%60);
        if(flag==1){
            flag=0;
            setTimeout(function(){
                sliding.style.left=460*(audio.currentTime/audio.duration)+"px";
                cur.style.width=460*(audio.currentTime/audio.duration)+5+"px";
                flag=1;
        },1000);
        }
    }else{
        controls[2].setAttribute("class","play fl shoutdown");
        controls[1].removeAttribute("class");
        controls[1].setAttribute("class","play fl");
         //自动播放下一曲
         if(audio.currentTime==audio.duration){
            if(now_sing==sing_list.length-1){
                now_sing=0;
            }else{
                now_sing+=1;
            }
            audio.src=sing_list[now_sing]["url"];
            audio.play();
        }
    }
}
//上一曲和下一曲
function prev_sing(){
    if(now_sing==0){
        now_sing=sing_list.length-1;
    }else{
        now_sing-=1;
    }
    prev_url=sing_list[now_sing]["url"];
    if(audio.paused){
        audio.src=prev_url;
    }else{
        audio.src=prev_url;
        audio.play();
    }
}
function next_sing(){
    if(now_sing==sing_list.length-1){
        now_sing=0;
    }else{
        now_sing+=1;
    }
    next_url=sing_list[now_sing]["url"];
    if(audio.paused){
        audio.src=next_url;
    }else{
        audio.src=next_url;
        audio.play();
    }
}
//自由点播
function auto_play(){
    now_sing=sing_list.length-1;
    if(audio.paused){
        audio.src=sing_list[now_sing]["url"];
    }else{
        audio.src=sing_list[now_sing]["url"];
        audio.play();
    }
}
//音乐控制面板是否锁定
var clocks=document.getElementsByClassName("zoom")[0].getElementsByTagName("a");
var play_control=document.getElementsByClassName("control")[0];
var play_zoom=document.getElementsByClassName("zoom")[0];
var clock=1;var complete=1;
clocks[0].onclick=function(){
    this.setAttribute("class","clock shoutdown");
    clocks[1].removeAttribute("class");
    clocks[1].setAttribute("class","unclock");
    clock=0;
}
clocks[1].onclick=function(){
    this.setAttribute("class","unclock shoutdown");
    clocks[0].removeAttribute("class");
    clocks[0].setAttribute("class","clock");
    clock=1;
}
play_control.onmouseleave=function(){
    if(!clock&&complete){
        complete=0;
        var hidden=setInterval(function(){
            play_control.style.bottom=parseInt(play_control.style.bottom)-1+"px";
            play_zoom.style.bottom=parseInt(play_control.style.bottom)-1+"px";
            if(parseInt(play_control.style.bottom)<=-45){
                clearInterval(hidden);
                complete=1;
            }
        },10);
    }
}
play_control.onmouseenter=function(){
    if(!clock&&complete){ 
        complete=0;
        var display=setInterval(function(){
            play_control.style.bottom=parseInt(play_control.style.bottom)+1+"px";
            play_zoom.style.bottom=parseInt(play_control.style.bottom)+1+"px";
            if(parseInt(play_control.style.bottom)>=0){
                clearInterval(display);
                complete=1;
            }
        },10);
    }
}
//nav互动
var nav1_list=document.getElementsByClassName("nav_list")[0].getElementsByTagName("li");
for(var i=0;i<nav1_list.length;i++){
    nav1_list[i].onclick=function(){
        for(var j=0;j<nav1_list.length;j++){
            nav1_list[j].getElementsByTagName("span")[0].className=" ";
        }
    this.getElementsByTagName("span")[0].className="list_sub";
}
}
var nav2_list=document.getElementsByClassName("nav_2")[0].getElementsByTagName("li");
for(var i=0;i<nav2_list.length;i++){
    nav2_list[i].onclick=function(){
        for(var j=0;j<nav2_list.length;j++){
            nav2_list[j].getElementsByTagName("a")[0].className=" ";
        }
    this.getElementsByTagName("a")[0].className="nav_2_active";
}
}
document.onkeypress=function(e){
    if(e.keyCode==32){
        if(audio.paused){
            audio.play();
        }else{
            audio.pause();
        }
        return false;
    }
}